package com.java.collection;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class MyArrayList implements List {
	private Object[] data;
	private int count;//表示列表的长度
	public MyArrayList(){
		super();
	}
	public MyArrayList(Object[] data){
		super();
		this.data=data;
		this.count=0;
	}
	
	public void add(int index, Object obj) {
		if(index<0||index>=count){
			throw new IndexOutOfBoundsException("数组下标越界异常！");
		}
		if(count==data.length){
			Object[] newdata=new Object[data.length*2];
			System.arraycopy(data, 0, newdata, 0, data.length);
			data=newdata;
		}
		for(int i=count-1;i>=index;i--){
			data[count]=data[i];
		}
		data[index]=obj;
		count++;
	}

	public boolean add(Object obj) {
		add(count,obj);
		return true;
	}

	public boolean addAll(Collection arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean addAll(int arg0, Collection arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	public void clear() {
		count=0;
	}

	public boolean contains(Object obj) {
		for(int i=0;i<this.data.length;i++){
			if(data[i].equals(obj)){
				return true;
			}
		}
		return false;
	}

	public boolean containsAll(Collection arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public Object get(int index) {
		if(index<0||index>=count){
			throw new ArrayIndexOutOfBoundsException();
		}
		return data[index];
	}

	public int indexOf(Object obj) {
		for(int i=0;i<data.length;i++){
			if(data[i].equals(obj)){
				return i;
			}
		}
		return -1;
	}

	public boolean isEmpty() {
		return count==0;
	}

	public Iterator iterator() {
		return new Iterator(){
			private int index=0;
			public boolean hasNext() {
				return index<count;
			}

			public Object next() {
				return data[index++];
			}

			public void remove() {
				MyArrayList.this.remove(--index);
			}
			
		};
	}

	public int lastIndexOf(Object o) {
		// TODO Auto-generated method stub
		return 0;
	}

	public ListIterator listIterator() {
		// TODO Auto-generated method stub
		return null;
	}

	public ListIterator listIterator(int index) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object remove(int index) {
		Object tempobj=data[index];
		for(int i=index;i<=count-1;i++){
			data[i]=data[i+1];
		}
		count--;
		return tempobj;
	}

	public boolean remove(Object obj) {
		int index=this.indexOf(obj);
		if(index!=-1){
			remove(index);
			return true;
		}
		return false;
	}

	public boolean removeAll(Collection arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean retainAll(Collection arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public Object set(int index, Object obj) {
		if(index<0||index>=count){
			throw new IndexOutOfBoundsException();
		}
		Object tempobj=data[index];
		data[index]=obj;
		return tempobj;
	}

	public int size() {
		return count;
	}

	public List subList(int fromIndex, int toIndex) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object[] toArray() {
		return this.data;
	}

	public Object[] toArray(Object[] arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	

}
